﻿using DirectShowLib.BDA;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ET.ScoreTeacherClient.Views
{
    /// <summary>
    /// CommandEditView.xaml 的交互逻辑
    /// </summary>
    public partial class CommandEditView : Window, INotifyPropertyChanged
    {
        public VoiceLibraryViewModel viewModel { get; set; }
        public static List<Operateitem> StepItemList = OperateitemDAL.OperateitemList();
        List<Operateitem> xinhaolist = StepItemList.Where(T => T.Describes.Contains("信号_")).ToList();
        public static List<DevBranches> branches = new List<DevBranches>();
        public CommandEditView()
        {
            InitializeComponent();
        }
        public CommandEditView(VoiceLibraryViewModel viewModel)
        {
            InitializeComponent();
            this.viewModel = viewModel;
        }
        #region INotifyPropertyChanged 接口实现
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string propName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }
        #endregion
        #region DicDevType
        private Dictionary<int, string> m_DicDevType;

        public Dictionary<int, string> DicDevType
        {
            get
            {
                return this.m_DicDevType;
            }
            set
            {
                this.m_DicDevType = value;
                this.OnPropertyChanged();
            }
        }
        #endregion

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            branches.Clear();
            branches.Add(new DevBranches() { type = 0, Describe = "NULL" });
            branches.Add(new DevBranches(){ type = 1, Describe = "车站" });
            branches.Add(new DevBranches() { type = 1, Describe = "全部车站" });
            branches.Add(new DevBranches() { type = 1, Describe = "集中站" });
            branches.Add(new DevBranches() { type = 1, Describe = "上级部门" });
            branches.Add(new DevBranches() { type = 1, Describe = "段信号楼" });
            branches.Add(new DevBranches() { type = 1, Describe = "X段派班室" });
            branches.Add(new DevBranches() { type = 1, Describe = "全线各站邻线调度" });
            branches.Add(new DevBranches() { type = 1, Describe = "清人车站" });
            branches.Add(new DevBranches() { type = 2, Describe = "列车" });
            branches.Add(new DevBranches() { type = 2, Describe = "通拨组" });
            cmbTypeBranch.ItemsSource = branches;
            List<DeviceType> deviceTypes = new List<DeviceType>();
            deviceTypes.Insert(0, new DeviceType() { ID = 0, TypeName = "未选择" });
            deviceTypes.Insert(1, new DeviceType() { ID = 5, TypeName = "车站" });
            deviceTypes.Insert(2, new DeviceType() { ID = 8, TypeName = "列车" });
            this.DicDevType = deviceTypes.ToDictionary(t => t.ID, t => t.TypeName);
            xinhaolist.Insert(0, new Operateitem() { Id = 0, Describes = "NULL" });
            xinhaolist.Add(new Operateitem() { Id = 200, Describes = "信号_设置列车换端" });//列表加入列车换端信号  
            cmbCommandStep.ItemsSource = xinhaolist;
            if(viewModel.CommandStep!=""&& viewModel.CommandStep != null&& viewModel.CommandStep != "NULL")
            {
                var xinhao = xinhaolist.Where(x => x.Describes == viewModel.CommandStep).ToList();

                if (xinhao.Count>0)
                {
                    viewModel.CommandStepid = xinhao[0].Id;
                }
               
            }
            else
            {
                cmbCommandStep.SelectedIndex = 0;
            }
            this.DataContext = this;
        }
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            switch(viewModel.DevType)
            {
                case 0:
                    viewModel.DevTypeName = "NULL";
                    break;
                case 5:
                    viewModel.DevTypeName = "车站";
                    break;
                case 8:
                    viewModel.DevTypeName = "列车";
                    break;
            }
            if(viewModel.CommandStepid!=0)
            viewModel.CommandStep = xinhaolist.Where(x => x.Id == viewModel.CommandStepid).First().Describes;
            if (viewModel.ID == 0)
            {
                int newId = 0;
                if (VoiceLibraryDAL.InsertVoiceLibrary(out newId, viewModel.Model))
                {
                    viewModel.ID = newId;
                    this.DialogResult = true;
                }
            }
            else
                if (VoiceLibraryDAL.UpdateVoiceLibrary(viewModel.Model))
                this.DialogResult = true;
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void cmbDevType_DropDownOpened(object sender, EventArgs e)
        {
            if (cmbDevType.SelectedIndex == 0)
            {
                this.cmbTypeBranch.ItemsSource = branches;
            }
            else if (cmbDevType.SelectedIndex == 1)
            {
                this.cmbTypeBranch.ItemsSource = branches.Where(T => T.@type!=2);
            }
            else if (cmbDevType.SelectedIndex == 2)
            {
                this.cmbTypeBranch.ItemsSource = branches.Where(T => T.@type!=1);
            }
        }
    }
    public class DevBranches
    {
        /// <summary>
        /// 1车站 2列车
        /// </summary>
        public int type { get; set; }
        public string Describe { get; set; }
    }
}
